cap log close

global path = "****"

local name= "excl_rest_cov_quality"
local date = "053020"
global logname = "$path/`name'_`date'_v2.log"

clear
set more off
set matsize 11000
cd "$path"
log using "$logname", replace

use "$path/Data/w2_codedd2017",clear
merge 1:1 tin dep_tin using  "$path/Data/penalty_sample.dta", gen(anyw22017)
replace prem=0 if prem==.
rename prem esi_premiums2017

replace age_2017=0 if age_2017<0
merge 1:1 tin dep_tin using "$path/Data/w2_codedd2018",gen(anyw22018)
rename prem esi_premiums2018

save "$path/temp",replace


use Data/f1095_2017,clear
collapse (sum) aptctotal slcsptotal premtotal,by(tin)
foreach var of varlist aptctotal slcsptotal premtotal{
replace `var'=0 if `var'==.
rename `var' `var'17
}
merge 1:m tin using "$path/temp",nogen 
save "$path/temp",replace

use Data/f1095_2018,clear
collapse (sum) aptctotal slcsptotal premtotal,by(tin)
foreach var of varlist aptctotal slcsptotal premtotal{
replace `var'=0 if `var'==.
rename `var' `var'18
}
merge 1:m tin using "$path/temp",nogen 
save "$path/temp",replace

gen long esi_premium=esi_premiums2017+esi_premiums2018
gen long exchange_premium=premtotal17+premtotal18


foreach var in exchange medicaid esi{

gen `var'1718 = `var'2017 + `var'2018
egen `var'1718hh=sum(`var'1718),by(tin)
}

egen esi_premium_hh=sum(esi_premium),by(tin)
egen notall16hh=sum(notall16),by(tin)

gen esi_premium_percap=esi_premium_hh/esi1718hh
gen exchange_premium_percap=exchange_premium/exchange1718hh 


cap erase "$path/`name'_`date'_v2.ster"

gen age_45_64 = (age_2017 >= 45 & age_2017 <= 64)


*Col 1: Exchange Coverage
reg exchange1718hh treatment if notall16hh == 0 & age_45_64 == 1&tin==dep_tin, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/`name'_`date'_v2.ster", append

*Col 2: Exchange Premiums
reg exchange_premium_percap treatment if notall16hh == 0 & age_45_64 == 1&tin==dep_tin, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/`name'_`date'_v2.ster", append

*Col 3: ESI Coverage
reg esi1718hh treatment if notall16hh == 0 & age_45_64 == 1&tin==dep_tin, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/`name'_`date'_v2.ster", append
			
*Col 4: ESI Premiums
reg esi_premium_percap treatment if notall16hh == 0 & age_45_64 == 1&tin==dep_tin, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/`name'_`date'_v2.ster", append			



log close

